package com.caiyouxi.user.util;


import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/11.
 */
public class CyxUtil {
    public static BaseResp buildSuccessResp(Object obj){

        BaseResp resp = new BaseResp();
        resp.setCode("SUCCESS");
        resp.setMsg("");
        resp.setData(obj);
        return resp;
    }

    public static Map<String, Object> buildResp(String code, String message) {
        Map<String, Object> resp = new HashMap<>();
        if (Strings.isEmpty(message)) {
            resp.put("code", "SUCCESS");
            resp.put("msg", "");
            return resp;
        } else {
            resp.put("code", code);
            resp.put("msg", message);
            return resp;
        }
    }

    public static Map<String, Object> setData(String name, Object val) {

        Map<String, Object> data = new HashMap<>();
        data.put(name, val);
        return data;
    }

    public static String generateUsername(String prefix) {

        return (Strings.isBlank(prefix) ? "CYX" : prefix.toUpperCase()) + Strings.alignLeft(R.random(0, 999999999), 9, '0') + R.random(0, 9);
    }

    public static BaseResp buildResultResp(Boolean b){

        BaseResp resp = new BaseResp();
        resp.setCode("SUCCESS");
        resp.setMsg("");
        Map<String, Object> map = new HashMap<>();
        map.put("result", b);
        resp.setData(map);
        return resp;
    }

    public static BaseResp buildErrorResp(String code, String msg){

        BaseResp resp = new BaseResp();
        resp.setCode(code);
        resp.setMsg(msg);
        return resp;
    }

    public static BaseResp buildSuccessMsgResp(Boolean b, String msg){

        BaseResp resp = new BaseResp();
        resp.setCode("SUCCESS");
        resp.setMsg(msg);
        Map<String, Object> map = new HashMap<>();
        map.put("result", b);
        resp.setData(map);
        return resp;
    }

    public static String buildPswd(String passwrod, String salt, String algorithm) {

        if (Strings.equalsIgnoreCase(algorithm, "MD5")) {
            return Lang.md5(Lang.md5(passwrod) + salt);
        } else if (Strings.equalsIgnoreCase(algorithm, "SHA")) {
            return Lang.sha1(Lang.sha1(passwrod) + salt);
        } else if (Strings.equalsIgnoreCase(algorithm, "SHA2")) {
            return Lang.sha256(Lang.sha256(passwrod) + salt);
        } else {
            return passwrod;
        }
    }

    public static Boolean authStatus(String authStatus) {

        switch (authStatus) {

            case "VERIFIED":
                return true;
            case "VERIFYING":
                return true;
            default:
                return false;
        }
    }

    public static String getLoginSource(String username) {

        if (Strings.isMobile(username))
            return "MOBILE";
        else if (Strings.isEmail(username))
            return "EMAIL";
        else
            return "USERNAME";
    }

    public static String checkCodeAction(String action) {

        switch (action) {
            case "REG":
                return "";
            case "RESET_PWD":
                return "";
            case "BIND":
                return "";
            case "UNBIND":
                return "";
            case "LOGIN":
                return "";
            case "PAYMENT":
                return "";
            default:
                return "不支持的方式";
        }
    }

}
